﻿using System;
using System.ServiceModel;
using Lypshare.Net.Server.Service;

namespace Lypshare.Net.Server
{
	/// <summary>
	/// Access point.
	/// </summary>
	/// <exception cref='ArgumentNullException'>
	/// Is thrown when an argument passed to a method is invalid because it is <see langword="null" /> .
	/// </exception>
    public class AccessPoint : IDisposable
    {
        // --------------------------------------------------------
        // Class Variables
        // --------------------------------------------------------


        /// <summary>
        /// The _service.
        /// </summary>
        private ServiceHost _service = null;


        /// <summary>
        /// The _name.
        /// </summary>
        private String _name = null;


        /// <summary>
        /// The _port.
        /// </summary>
        private int _port = 0;


        // --------------------------------------------------------
        // Class Properties
        // --------------------------------------------------------


        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>
        /// The name.
        /// </value>
        public String Name
        {
            get { return _name; }

        } // Property: Name


        /// <summary>
        /// Gets the port.
        /// </summary>
        /// <value>
        /// The port.
        /// </value>
        public int Port
        {
            get { return _port; }

        } // Property: Port


        // --------------------------------------------------------
        // Class Functions
        // --------------------------------------------------------


        /// <summary>
        /// Initializes a new instance of the <see cref="Lypshare.Net.Server.AccessPoint"/> class.
        /// </summary>
        /// <param name='address'>
        /// Address.
        /// </param>
        /// <param name='port'>
        /// Port.
        /// </param>
        /// <param name='name'>
        /// Name.
        /// </param>
        /// <exception cref='ArgumentNullException'>
        /// Is thrown when an argument passed to a method is invalid because it is <see langword="null" /> .
        /// </exception>
        public AccessPoint(String address, int port, String name)
        {
            if (address == null) throw new ArgumentNullException("address");
            if (name == null) throw new ArgumentNullException("name");

            _service = new ServiceHost(typeof(AccessService));

            _service.AddServiceEndpoint(typeof(IAccessService),
                new NetTcpBinding(SecurityMode.None), "net.tcp://" + address 
                    + ":" + port + "/" + name);

            _name = name;
            _port = port;

        } // Constructor


        /// <summary>
        /// Open this instance.
        /// </summary>
        public void Open()
        {
            _service.Open();

        } // Open


        /// <summary>
        /// Close this instance.
        /// </summary>
        public void Close()
        {
            Dispose();

        } // Close


        /// <summary>
        /// Releases all resource used by the <see cref="Lypshare.Net.Server.AccessPoint"/> object.
        /// </summary>
        /// <remarks>
        /// Call <see cref="Dispose"/> when you are finished using the <see cref="Lypshare.Net.Server.AccessPoint"/>.
        /// The <see cref="Dispose"/> method leaves the <see cref="Lypshare.Net.Server.AccessPoint"/> in an unusable
        /// state. After calling <see cref="Dispose"/>, you must release all references to the
        /// <see cref="Lypshare.Net.Server.AccessPoint"/> so the garbage collector can reclaim the memory that the
        /// <see cref="Lypshare.Net.Server.AccessPoint"/> was occupying.
        /// </remarks>
        public void Dispose()
        {
            if(_service != null)
            {
                _service.Close();
                _service = null;

            } // if(_service != null)
        } // Dispose

    } // class AccessPoint
} // namespace Lypshare.Net.Server